

using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UtilLib;
using UtilPublic.Serialization;
using UtilPublic.ServerLoger;

namespace ConfClient
{
    /// <summary>
    /// 配置类公用接口
    /// </summary>
    public interface IConfigBase
    {
        object GetKey();
    }

    /// <summary>
    /// 配置文件
    /// </summary>
    public static partial class Conf
    {
        /// <summary>
        /// 配置文件目录
        /// </summary>
        public static string ConfPathJson = @"Config/";

        public static string ConfListSeparator = UtilEnum.分隔符竖线;
        public static string ConfDictionarySeparator1 = UtilEnum.分隔符竖线;
        public static string ConfDictionarySeparator2 = UtilEnum.分隔符下划线;

        /// <summary>
        /// 初始化配置
        /// </summary>
        /// <returns></returns>
        public static bool InitConf()
        {
            bool result = true;

            var StopwatchProcess = Stopwatch.StartNew();
            result = InitConfSettings();
            StopwatchProcess.Stop();
            loger.Info($"InitConfSettings {StopwatchProcess.Elapsed}");

            if (result)
            {
                loger.Info("读取策划配置文件成功");
            }
            else
            {
                loger.Fatal("读取策划配置文件失败");
                return result;
            }

            return result;
        }

        /// <summary>
        /// 读取Dictionary配置
        /// </summary>
        /// <param name="ConfName"></param>
        /// <returns></returns>
        public static bool ReadConfig<T1, T2>(string Name, ref Dictionary<T1, T2> confDic)
        {
            return ReadJsonDict(Name, ref confDic, ConfPathJson);
        }
        /// <summary>
        /// 读取字典
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="Name">配置文件名称</param>
        /// <param name="confDic">配置</param>
        /// <param name="pathName">目录</param>
        /// <returns></returns>
        public static bool ReadJsonDict<T1, T2>(string Name, ref Dictionary<T1, T2> confDic, string pathName)
        {
            string ConfName = $"{pathName}{Name}";
            try
            {
                try
                {
                    var text = Resources.Load<TextAsset>(ConfName).text;
                    if (text.IsNullOrEmpty())
                    {
                        text = "";
                    }

                    confDic = Jsh.Jobj<Dictionary<T1, T2>>(text);

                    //loger.Info($"读取配置-> {ConfName,-40} <-成功 共{confDic.Count}项");
                }
                catch (Exception e)
                {
                    loger.Fatal($"读取配置-> {ConfName,-40} <-失败", e);
                    return false;
                }
            }
            catch (Exception)
            {
                loger.Fatal($"读取配置-> {ConfName,-40} <-失败2");
                return false;
            }
            return true;
        }
    }
}